$(document).ready(function() {
				var playing = false;
				var totalTimeWatched = 0;
				var lastTimeCalled = new Date();
				var States = {
						playingEnded : 0,
						playingStared : 1,
						playingPaused : 2,
						playingPeriodicEvent : -2,
						playingResumed : 3,
						playingStopped : 4
				}
				jQuery(window).unload(function() {
					sendTotalTimeWatchedToServer();
				});
				setInterval(function() {
					playerStateChanged(States.playingPeriodicEvent);
				}, 5000);
				function sendTotalTimeWatchedToServer() {
					if(totalTimeWatched > 0) {
						jQuery.ajax({
							type: "POST",
							async: false,
							url: "saveTotalTimeWatched.php",
							data: "totalTimeWatched="+totalTimeWatched+"&videoName="+jQuery("#videoName").val()
						});
					}
				}
				function playerStateChanged(state) {
					if (state == States.playingStared) {
						playing = true;
					} else if (state == States.playingPaused) {
						playing = false;
					} else if (state == States.playingResumed) {
						playing = true;
					} else if (state == States.playingPeriodicEvent) {
						if (playing) {
							totalTimeWatched+= ((new Date() - lastTimeCalled)) / 1000.0;
						}
						lastTimeCalled = new Date();
					} else if (state == States.playingEnded) {
						playing = false;
						sendTotalTimeWatchedToServer();
						totalTimeWatched = 0;
					} else if (state == States.playingStopped) {
						playing = false;
					}
				}
				flowplayer('flowplayer', 'flowplayer-3.2.7.swf', {
					clip : {
						autoPlay : false, 
						autoBuffering : false,
						onStart : function(clip) {
							playerStateChanged(States.playingStared);
						},
						onPause : function(clip) {
							playerStateChanged(States.playingPaused);
						},
						onResume : function(clip) {
							playerStateChanged(States.playingResumed);
						},
						onStop : function (clip) {
							playerStateChanged(States.playingStopped);
						},
						onFinish : function(clip) {
							playerStateChanged(States.playingEnded);
						}
					}
				})
			});